package com.huluobo.a2307monthdemo2.login

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.appcompat.app.AlertDialog
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2307monthdemo2.MainActivity
import com.huluobo.a2307monthdemo2.R
import com.huluobo.a2307monthdemo2.databinding.ActivityLoginBinding
import com.huluobo.lib_base.bean.Money
import com.huluobo.lib_base.bean.UserBean
import com.huluobo.lib_base.mvvm.BaseActivity
import com.huluobo.lib_base.mvvm.MessageEvent
import com.huluobo.lib_base.net.Const
import com.hyphenate.EMCallBack
import com.hyphenate.chat.EMClient
import com.hyphenate.notification.core.b
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    private var rememberPassword = false
    override fun initData() {
        vm.loginSuccess.observe(this) {
            EMClient.getInstance().login(it.username, it.password, object : EMCallBack {
                override fun onSuccess() {
                    ToastUtils.showLong("欢迎回来:${it.nickname}")
                    SPUtils.getInstance().put(Const.PARAM_TOKEN, it.token)
                    SPUtils.getInstance().put(Const.PARAM_USERNAME, it.username)
                    val money = Money(username = it.username, money = 10000f)
                    vm.insertMoney(money)
                    if (rememberPassword) {
                        SPUtils.getInstance().put(Const.PARAM_USERNAME, it.username)
                        SPUtils.getInstance().put(Const.PARAM_PASSWORD, it.password)
                    } else {
                        SPUtils.getInstance().remove(Const.PARAM_USERNAME)
                        SPUtils.getInstance().remove(Const.PARAM_PASSWORD)
                    }
                    startAct(MainActivity::class.java)
                }

                override fun onError(code: Int, error: String?) {
                    ToastUtils.showLong("登录环信失败:$code,$error")
                }
            })
        }
    }

    override fun initView() {
        if (SPUtils.getInstance().getString(Const.PARAM_USERNAME).isNotEmpty() && SPUtils.getInstance()
                .getString(Const.PARAM_PASSWORD).isNotEmpty()
        ) {
            binding.loginUsernameEt.text.insert(0, SPUtils.getInstance().getString(Const.PARAM_USERNAME))
            binding.loginPasswordEt.text.insert(0, SPUtils.getInstance().getString(Const.PARAM_PASSWORD))
            binding.loginRememberPassword.isChecked = true
            rememberPassword = true
        }

        binding.loginBtn.setOnClickListener {
            if (binding.loginContractCb.isChecked) {
                val username = binding.loginUsernameEt.text.toString()
                val password = binding.loginPasswordEt.text.toString()

                if (username.isNotEmpty() && password.isNotEmpty()) {
                    val map = mapOf(
                        Const.PARAM_USERNAME to username,
                        Const.PARAM_PASSWORD to password
                    )
                    vm.login(map)
                } else {
                    binding.loginPasswordEt.error = "用户名密码不能为空"
                }
            } else {
                ToastUtils.showLong("请同意用户协议")
            }
        }

        binding.loginRememberPassword.setOnCheckedChangeListener { _, isChecked ->
            rememberPassword = isChecked
        }

        val contract = "同意《用户协议》"
        val builder = SpannableStringBuilder(contract)

        val blue = ForegroundColorSpan(Color.BLUE)
        builder.setSpan(blue, 2, contract.length, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.loginContractTv.text = builder

        binding.loginContractTv.setOnClickListener {
            AlertDialog.Builder(this).setTitle("用户协议")
                .setMessage("用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议用户协议")
                .setPositiveButton("确认") { dialog, _ ->
                    binding.loginContractCb.isChecked = true
                    dialog.dismiss()
                }.setNegativeButton("取消") { dialog, _ ->
                    dialog.dismiss()
                }.show()
        }

        binding.loginToRegister.setOnClickListener {
            startAct(RegisterActivity::class.java)
        }
    }

    override fun onEvent(messageEvent: MessageEvent) {
        super.onEvent(messageEvent)
        val userBean = Gson().fromJson(messageEvent.message, UserBean::class.java)
        binding.loginUsernameEt.text.insert(0, userBean.username)
        binding.loginPasswordEt.text.insert(0, userBean.password)
    }
}